/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xdialer.h
 *
 * Purpose: dialer base API define
 *
 * Developer:
 *   wen.gu , 2023-10-25
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_COM_XDIALER_H__
#define __XBUS_COM_XDIALER_H__

#include <string>
#include <memory>
#include <functional>

#include "xbus/core/xtype.h"
#include "xbus/core/xresult.h"
#include "xbus/core/xthread.h"
#include "xbus/com/xtypes.h"
#include "xbus/com/xmessage.h"
#include "xbus/com/xsession.h"

namespace xbus::com {

class XDialer;
using XDialerSp = std::shared_ptr<XDialer>;

class XDialer {
public:
    using XResultCode = core::XResultCode;
    using OnFindServiceHandler = std::function<void(XDialerSp)>;

public:
    XDialer(XSessionSp session);
    virtual ~XDialer();
public:
    virtual XResultCode start(const XClientStartParam& param);
    virtual XResultCode stop();
    virtual core::XResult<XMessageSp> alloc(XMessageType type, MessageId msg_id, uint32_t payload_size, const uint8_t* payload = nullptr);

public:
    virtual XResultCode send(XMessageSp msg);
    virtual core::XResult<XMessageSp> recv(int32_t wait_ms = core::WaitType::WaitForever);

public:
    static core::XResult<XDialerSp> FindService(const std::string& url, int32_t port = 0, int32_t timeout_ms = core::WaitType::NoWait);
    static core::XResult<FindServiceHandle> StartFindService(const std::string& url, int32_t port, OnFindServiceHandler handler);
    static XResultCode StopFindService(const FindServiceHandle& handle);

protected:
    uint32_t reconnect_time_interval_ms_ = XBUS_DISABLE_RECONNECTION;
    XSessionSp session_ = nullptr;
};

} //namespace xbus::com


#endif /** !__XBUS_COM_XDIALER_H__ */